package servcie;

import domain.Student;
import domain.Task;
import domain.Teacher;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class TeacherTask extends TaskScheduleImpl{
    private final Teacher teacher;
    private final ArrayList<Student> arrayList;
    private final Scanner scanner;
    public TeacherTask(Teacher teacher,ArrayList<Student> arrayList,Scanner scanner) {
        this.teacher = teacher;
        this.arrayList=arrayList;
        this.scanner=scanner;
    }
    private void addStudent(){
        System.out.println("来个名字");
        String name=scanner.next();     //添加学生姓名
        int size = arrayList.size()+1;  //判断集合里已有人数给予学号
        String id;
        if (size<10){       //判断位数更改学号格式
             id="00"+size;
        }else if (size>10){
             id="0"+size;
        }else {
             id=""+size;
        }
        arrayList.add(new Student(name,id));
        System.out.println("添加成功");
    }       //添加学生功能
    private void collectTask(){
        arrayList.forEach(student -> {
            if (student.isFlag()){
                Task task = student.getTask();
                System.out.println(student.getName()+":"+task.getConclusion()+','+task.getHomeWork()+','+task.getPreview());
            }else {
                System.out.println(student.getName()+": 未完成老师安排任务!");
            }
        });
    }       //收集任务功能
    private void deleteStudent(){
        while (true){
            System.out.println("要删除学生的学号");
            String del=scanner.next();
            Iterator<Student> iterator = arrayList.iterator();//遍历查找匹配学生
            while (iterator.hasNext()){
                if (iterator.next().getStuId().equals(del)){
                    iterator.remove();
                    System.out.println("删除成功");
                    return; //删完走人
                }
            }
            System.out.println("没这人");      //如果没找到执行后续操作
            System.out.println("还删不删？");
            System.out.println("1、删");
            System.out.println("2、不删");
            int select=scanner.nextInt();
            if (select==1){

            }else if (select==2){
                return;
            }else {
                System.out.println("乱输十八，那你别删了");
                return;
            }
        }
    }       //删除学生功能
    private void distributedTask(){
        Task task=new Task();
        String txt="每日总结需完成百分比:";
        System.out.println(txt+"数据%");
        String pinjie=scanner.next();
        task.setConclusion(txt+pinjie);
        txt="每日作业需完成百分比:";
        System.out.println(txt+"数据%");
        pinjie=scanner.next();
        task.setHomeWork(txt+pinjie);
        txt="每日预习需完成百分比:";
        System.out.println(txt+"数据%");
        pinjie=scanner.next();
        task.setPreview(txt+pinjie);
        teacher.setTask(task);
        teacher.setFlag(true);
    }       //派发任务功能
    private void selectAllStudents(){
        arrayList.forEach(System.out::println);     //打印学生信息
    }       //查找所有学生
    @Override
    public void task() {
        while (true){
            System.out.println("---------欢迎进入老师系统---------");
            System.out.println("1 查看学生");
            System.out.println("2 添加学生");
            System.out.println("3 移除学生");
            System.out.println("4 派发任务");
            System.out.println("5 查看任务");
            System.out.println("6 收集任务");
            System.out.println("7 退出老师管理系统");
            System.out.println("请选择任务安排:");
            int select=scanner.nextInt();
            switch (select){
                case 1:
                    selectAllStudents();
                    break;
                case 2:
                    addStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    distributedTask();
                    break;
                case 5:
                    if (teacher.isFlag()){
                        viewTask(teacher);
                    }else {
                        System.out.println("您还未下发任务!");
                    }
                    break;
                case 6:
                    if (teacher.isFlag()){
                        collectTask();
                    }else {
                        System.out.println("您还未下发任务!");
                    }
                    break;
                case 7:
                    return;
                default:
                    System.out.println("乱输十八？扇你");
            }
        }


    }       //核心任务方法
}
